local table_name = "card"
local query_sql = "SELECT `create_item_id`, `apply_time` FROM `%s` WHERE card_id = '%s'"
local update_sql = "UPDATE `%s` SET `apply_time` = '%s' WHERE `card_id` = '%s' AND `apply_time` = 0"

local M = {}

---! 数据加载
function M.load(conn, msg)
    local invite_code = table.unpack(msg.data)

    local cmd = string.format(query_sql, table_name, invite_code)
    local cur = conn:execute(cmd)
    local row = cur:fetch({}, "a")
    cur:close()

    if not row then
        return
    end

    local reply = {}
    reply.gift_id = tonumber(row.create_item_id)
    reply.apply_time = tonumber(row.apply_time)
    return reply
end

---! 数据更新
function M.update(conn, msg)
    local invite_code, apply_time = table.unpack(msg.data)

    local cmd = string.format(update_sql, table_name, apply_time, invite_code)
    -- print(cmd)

    local affected_rows = conn:execute(cmd)
    return affected_rows > 0
end

return M
